package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * @version 1.0
 * @Author 杨旭康
 * @Date 2022/10/30 11:44
 * @question:
 */

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    //注入mapper
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 用户登录
     * @param employee
     * @return
     */
    @Override
    public Employee login(Employee employee) {

        //条件判断 用户用户名
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != employee.getUsername(),Employee::getUsername,employee.getUsername());

        //调用方法判断
        Employee emp = employeeMapper.selectOne(queryWrapper);

        return emp;
    }

    /**
     * 添加员工
     * @param employee
     */
    @Override
    public void saveEmp(Employee employee) {
        //设置md5加密密码,给初始值123456
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        employeeMapper.insert(employee);
    }

    /**
     * 分页条件查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<Employee> getByPage(int page,int pageSize,String name) {

        //分页工具MP提供(里边含有前端需要的 records,total等)
        Page<Employee> pageInfo = new Page<>(page,pageSize);
        //条件查询器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //模糊查询
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //排序
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        employeeMapper.selectPage(pageInfo,queryWrapper);

        return pageInfo;
    }

    /**
     * 修改员工信息
     * @param employee
     */
    @Override
    public void updateByID(Employee employee) {

        employeeMapper.updateById(employee);
    }

    /**
     * 根据id查询--回显数据
     * @param id
     */
    @Override
    public Employee getById2(Long id) {
        Employee employee = employeeMapper.selectById(id);
        return employee;
    }




}
